﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using Host.Model;


namespace Host.DB
{
    class DBLoginMenu
    
    {
        public User Login(String Username, String Password)
        {
            User userData = null;

            SqlConnection con = new SqlConnection();      
            con.ConnectionString = "Data Source=balder.ucn.dk;User Id=dmaa0213_4;Initial Catalog=dmaa0213_4;Password=MaaGodt; Integrated Security=false";
            con.Open();


            String query = "SELECT ID, fName, lName, address, zipCode, city, licensPlate, phoneNumber, email, userName, password, rank FROM Users WHERE Username=@user AND Password =@password";
            SqlCommand cmd = new SqlCommand(query, con);
            cmd.Parameters.Add(new SqlParameter("@user", Username));
            cmd.Parameters.Add(new SqlParameter("@password", Password));

            SqlDataReader dr = cmd.ExecuteReader();

            if (dr.HasRows == true)
            {
                dr.Read();
                int ID = Int32.Parse(dr["ID"].ToString());
                String fName = dr["fname"].ToString();
                String lName = dr["lName"].ToString();
                String address = dr["address"].ToString();
                int zipCode = Int32.Parse(dr["zipCode"].ToString());
                String city = dr["city"].ToString();
                String licensPlate = dr["licensPlate"].ToString();
                String phoneNumber = dr["phoneNumber"].ToString();
                String email = dr["email"].ToString();
                String userName = dr["userName"].ToString();
                String password = dr["password"].ToString();
                int rank = Int32.Parse(dr["rank"].ToString());

                userData = new User(ID, fName, lName, address, zipCode, city, licensPlate, phoneNumber, email, userName, password, rank);
            }
             return userData;
        }
        }
    }

    

